﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Research.DynamicDataDisplay.Common;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.Charts;
using System.Data.SqlClient;
using System.Data;
using Microsoft.SqlServer.Types;

namespace GeometryViewerSampleApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //plotter.Viewport.Visible = new Rect(-0.2, -1.2, 1.4, 2.4);
            SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Graphs;Integrated Security=True");
            SqlCommand cmd1;
            SqlCommand cmd2;
            try
            {
                conn.Open();
                cmd1 = conn.CreateCommand();
                cmd1.CommandType = CommandType.Text;
                cmd1.CommandText = "SELECT dbo.AdjacencyPolygon(graph, 0.2) from Graphs where id =6";
                cmd2 = conn.CreateCommand();
                cmd2.CommandType = CommandType.Text;
                cmd2.CommandText = "SELECT dbo.AdjacencyPolygon(graph, 0.2) from Graphs where id =7";
                SqlGeometry g1 = LoadFromDatabase(cmd1);
                SqlGeometry g2 = LoadFromDatabase(cmd2);

                GeometryViewer geometryViewer1 = new GeometryViewer(g1, Brushes.Blue, true);
                GeometryViewer geometryViewer2 = new GeometryViewer(g2, Brushes.Red, false);
                plotter.Children.Add(geometryViewer1);
                plotter.Children.Add(geometryViewer2);
                conn.Close();
            }
            catch (SqlException ex)
            {
                
            }
            /*Rect sizeRect = geometryViewer1.BoundingBox;
            sizeRect.Union(geometryViewer2.BoundingBox);
            plotter.Viewport.Visible = sizeRect;*/
            resetPlotter();

        }

        private SqlGeometry LoadFromDatabase(SqlCommand cmd)
        {
            using (cmd)
            {
                try
                {
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        rdr.Read();

                        int geomCol = 0;
                        if (rdr.FieldCount > 2)
                        {
                            return null;
                        }
                        if (rdr.FieldCount == 2)
                        {
                            geomCol = 1;
                        }

                        if (!(rdr[geomCol] is SqlGeometry))
                        {
                            return null;
                        }
                        do
                        {
                            if (rdr[geomCol] != DBNull.Value)
                            {
                                return (SqlGeometry)rdr[geomCol];
                            }
                        } while (rdr.Read());
                        //txtStatus.Text = string.Format("Fetched {0} records.", count);
                    }
                }
                catch (SqlException ex)
                {
                    //txtStatus.Text = string.Format("Could not execute SQL. The error returned was: {0}", ex.Message);
                }
            }
            return null;
        }

        private void buttonXPlus_Click(object sender, RoutedEventArgs e)
        {
            plotter.Visible = plotter.Visible.ZoomX(plotter.Visible.GetCenter(), 0.9);
        }

        private void buttonXMinus_Click(object sender, RoutedEventArgs e)
        {
            plotter.Visible = plotter.Visible.ZoomX(plotter.Visible.GetCenter(), 1.1);
        }

        private void buttonYPlus_Click(object sender, RoutedEventArgs e)
        {
            plotter.Visible = plotter.Visible.ZoomY(plotter.Visible.GetCenter(), 0.9);
        }

        private void buttonYMinus_Click(object sender, RoutedEventArgs e)
        {
            plotter.Visible = plotter.Visible.ZoomY(plotter.Visible.GetCenter(), 1.1);
        }
        private void resetPlotter()
        {
            Rect sizeRect = new Rect();
            foreach (var child in plotter.Children)
            {
                if (child.GetType() == typeof(GeometryViewer)) 
                {
                    GeometryViewer gv = (GeometryViewer)child;
                    sizeRect.Union(gv.BoundingBox);
                }
            }
            plotter.Viewport.Visible = sizeRect;
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            resetPlotter();
        }
    }
}
